
cd "/disk/agedisk3/medicare.work/chandra-DUA52080/pragya-dua52080/replication/"
************ SETUP ******************
set more off
capture log close
clear
version 14.0

log using summstats.log, replace

*** Sections of the code
global HOSPITAL_LEVEL = 1
global PATIENT_LEVEL = 1


************ summary stats - quality ******************
	if ($HOSPITAL_LEVEL){

		matrix summ = J(7,4,.)
		matrix colnames summ = ami_black_baseline ami_white_baseline ami_black_endline ami_white_endline
		matrix rownames summ = surv_sd  patients_allraces_mean patients_allraces_sd patients_racespecific_mean patients_racespecific_sd hospitals_racespecific hospitals_allraces
		local c = 1
		
		foreach per in 1 4{

		* do summary stats from the original sample
		use * if yidx == 1 | yidx == 4 using   Intermediate_Output_Not_Exportable/bs0.dta, clear
		
		** keep the balanced sample
		gen byte insamp = longsurv_ami_fere_a != . 
		bysort pn: egen num_times_appear = sum(insamp)
		keep if num_times_appear == 2				
		keep if yidx == `per' 


		foreach heto in b w{

			* underlying sd
			** Must have quality measure ** 
			summ longsurv_ami_uvar_a  
			matrix summ[1,`c'] = sqrt(r(mean))

			** Total number of patients
			summarize npatients_a_ami_tot_act , detail
			matrix summ[2, `c'] = r(mean)
			** Sd patients per hospital
			matrix summ[3, `c'] = r(sd)	
		
			** Index event counts by race ** 
			** Here we include all hospitals (including those with 0 pats) ** 
			summarize npatients_`heto'_ami_tot_act , detail
			** Avg num patients per hospital 
			matrix summ[4, `c'] = r(mean)
			** Sd patients per hospital
			matrix summ[5, `c'] = r(sd)

			summarize npatients_`heto'_ami_tot_act if npatients_`heto'_ami_tot_act != 0 & npatients_`heto'_ami_tot_act !=. , detail
			** Num of hospitals with a growth rate in full period
			matrix summ[6, `c'] = r(N)
			
			** nhospitals with > 0 patients
			summ longsurv_ami_eb_a 
			matrix summ[7,`c'] = r(N)
	
		local c = `c' + 1
		}		
		}
		
		capture noisily mkdir Exportable_Results/
		capture noisily mkdir Exportable_Results/Table1/
		putexcel A1=matrix(summ, names) using  Exportable_Results/Table1/Table1_PanelB.xlsx, replace
		
}










if ($PATIENT_LEVEL){

	matrix summ = J(12,4,.)
	matrix colnames summ = black_baseline white_baseline black_endline white_endline 
	matrix rownames summ = surv age sd_age female history_heart_failure history_stroke history_copd Num_comos sd_numcomos distance_less100 sd_distance_less100 pats  

	use * if yidx == 1 | yidx == 4 using  Intermediate_Output_Not_Exportable/bs0.dta, clear
	gen byte insamp = longsurv_ami_fere_a != . 
	bysort pn: egen num_times_appear = sum(insamp)
	keep if num_times_appear == 2				
	keep pn
	duplicates drop 
	tempfile provider_list
	save `provider_list'


	clear
	use * if yidx == 1 | yidx == 4 using  Intermediate_Output_Not_Exportable/ami100.dta, replace
	generate num_comos = como_hyper + como_stroke + como_cervas + como_renal + como_dialysis + como_COPD + como_pnuemo + como_diabetes + como_protein + como_dementia + como_FDLsDis + como_periph + como_metaCancer + como_trauma + como_subs + deprecate_card_hf + deprecate_card_hyperhd + card_ua + card_cathero + card_respFal  + card_valve + card_arrhythmia	
	merge m:1 pn using `provider_list', generate(provider_match) assert(match master) keep(match)

	local c = 1
	local r = 1
	generate count = 1
	
	foreach per in 1 4{	
	foreach heto in b w{

	summarize surv_30 if yidx == `per' & `heto' 
	matrix summ[`r', `c'] = r(mean)
	local surv_30_local = r(mean)
	summarize ageAtdiag if yidx == `per' & `heto' 
	matrix summ[`r'+1, `c'] = r(mean)
	matrix summ[`r'+2, `c'] = r(sd)
	summarize male if yidx== `per' & `heto' 
	matrix summ[`r'+3, `c'] = 1-r(mean)
	summarize deprecate_card_hf if yidx == `per' & `heto'
	matrix summ[`r'+4, `c'] = r(mean)
	summarize como_stroke if yidx == `per' & `heto' 
	matrix summ[`r'+5, `c'] = r(mean)	
	summarize como_COPD if yidx == `per' & `heto' 
	matrix summ[`r'+6, `c'] = r(mean)	
	summarize num_comos if yidx == `per' & `heto' 
	matrix summ[`r'+7, `c'] = r(mean)
	matrix summ[`r'+8, `c'] = r(sd)
	summarize distance if yidx == `per' & `heto'  & (distance <= 100 & distance != . ) 
	matrix summ[`r'+9, `c'] = r(mean)
	matrix summ[`r'+10, `c'] = r(sd)
	summarize count if yidx == `per' & `heto' 
	matrix summ[`r'+11, `c'] = r(sum)	

	local c = `c'+1		
				
	}
	}

	capture noisily mkdir Exportable_Results/
	capture noisily mkdir Exportable_Results/Table1/	
	putexcel A1=matrix(summ, names) using  Exportable_Results/Table1/Table1_PanelA.xlsx, replace



}



